// pages/message/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageList:[],
    //当前页面
    current: 1,
    //每页的最大数
    size: 8,
    //总页面
    pages: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '消息详情'
    })
    console.log(options)
    var senderId = options.sender
    this.setData({
      senderId: senderId
    })
    this.getMessageList()
  },
  
  getMessageList: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var messageList = this.data.messageList
    wx.request({
      url: app.host + "/message/listBySender",
      method: 'get',
      data:{
        senderId: that.data.senderId,
        current: that.data.current,
        size: that.data.size
      },
      header: app.defaultHeader,
      complete(res) {
        console.log(res)
        wx.hideLoading()
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            var result = messageList.concat(res.data.data.records)
            that.setData({
              messageList: result,
              pages: res.data.data.pages,
              current: res.data.data.current
            })
          } else {
            that.setData({
              messageList: []
            })
          }
        } else {
          wx.showToast({
            title: "请求服务器失败，请检查网络!",
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },

  /** 触底加载更多 */
  onReachBottom: function () {
    var current = this.data.current
    var pages = this.data.pages
    //如果当前页面小于总页面数，则加一页，再请求
    if (current < pages) {
      console.log("加载更多....")
      this.setData({
        current: current + 1
      })
      this.getMessageList();
    } else {
      console.log("没有更多数据了...")
    }
  }


})